imprt scrapy
 
class PriceSpider(scrapy.Spider):
    # 爬虫的名字
    name = "price"
    # 允许的域名
    allowed_domains = ["techlabplt.com"]
    # 起始的URL地址
    start_urls = ["http://www.techlabplt.com:8080/BD-PC/priceList?pageId=1"]
 
    # 在parse方法内实现爬取数据的逻辑
    def parse(self, response):
        # 获取当前页内的所有农副产品的相关信息
        price_list = response.xpath("//*[@id='tableBody']/tr")
        # 遍历price_list 获取每一项内的数据值
        for price in price_list:
            tmp = {}
          # 判断选择器对象的值是不是存在，如果存在就提取出来，不存在赋空置
            if price.xpath("./td[1]/text()"):
                tmp['大类'] = price.xpath("./td[1]/text()")[0].extract()
            else:
                tmp['大类'] = ''
            if price.xpath("./td[2]/text()"):
                tmp['小类'] = price.xpath("./td[2]/text()")[0].extract()
            else:
                tmp['小类'] = ''
            if price.xpath("./td[3]/text()"):
                tmp['名称'] = price.xpath("./td[3]/text()")[0].extract()
            else:
                tmp['名称'] = ''
            if price.xpath("./td[4]/text()"):
                tmp['最低价'] = price.xpath("./td[4]/text()")[0].extract()
            else:
                tmp['最低价'] = ''
            if price.xpath("./td[5]/text()"):
                tmp['平均价'] = price.xpath("./td[5]/text()")[0].extract()
            else:
                tmp['平均价'] = ''
            if price.xpath("./td[6]/text()"):
                tmp['最高价'] = price.xpath("./td[6]/text()")[0].extract()
            else:
                tmp['最高价'] = ''
            if price.xpath("./td[7]/text()"):
                tmp['规格'] = price.xpath("./td[7]/text()")[0].extract()
            else:
                tmp['规格'] = ''
            if price.xpath("./td[8]/text()"):
                tmp['来源'] = price.xpath("./td[8]/text()")[0].extract()
            else:
                tmp['来源'] = ''
            if price.xpath("./td[9]/text()"):
                tmp['单位'] = price.xpath("./td[9]/text()")[0].extract()
            else:
                tmp['单位'] = ''
            if price.xpath("./td[10]/text()"):
                tmp['更新日期'] = price.xpath("./td[10]/text()")[0].extract()
            else:
                tmp['更新日期'] = ''
            # 返回数据
            yield tmp
